package com.kzd.gtgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.jetpack.manager.IndexRepository
import com.module.lemlin.http.*

class HomeViewModel(application: Application) : BaseViewModel(application) {

    private val mIndexRepository: IndexRepository = IndexRepository()

    private var page: Int = 1
    private var limit: Int = 20

    private var start = 0L


    val changeBarStyleLiveData = MutableLiveData<Int>()
    fun homeChangeBarStyle(distance: Int) {
        changeBarStyleLiveData.postValue(distance)
    }

    val indexHomeLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<HomeIndex>>>()
    fun homeIndex() {
        setRxLifeScope(SubscribeViewModel(indexHomeLiveData)) {
            mIndexRepository.homeIndex()
        }
    }

    val reserveChangeLiveData = MutableLiveData<HttpResponseBody<ReserveChangeResponse>>()
    fun reserveChange(gid: String, type: String, position: Int) {
        setRxLifeScope(object : SubscribeViewModel<ReserveChangeResponse>(reserveChangeLiveData) {
            override fun onSuccess(resp: ReserveChangeResponse) {
                super.onSuccess(resp.apply { this.position = position })
            }
        }) {
            mIndexRepository.reserveChange(gid, type)
        }
    }


    val homeDoingsLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<Doings<GameDoing>>>>()
    var mGameLayout = ""
    fun homeDoings(
        gameLayout: String,
        start: Long = 0,
        page1: Int = 0,
        isLoadMore: Boolean = false,
    ) {
        this.mGameLayout = gameLayout
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataT<Doings<GameDoing>>>(homeDoingsLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<Doings<GameDoing>>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            if (page1 > 0) {
                page = if (page >= 2) page else page1
            }
            mIndexRepository.homeDoings(
                gameLayout, start.toString(), page.toString(), limit.toString()
            )
        }
    }

    val homeCouponAreaLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameCoupon>>>()
    fun homeCouponArea(keyword: String? = null, isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GameCoupon>>(homeCouponAreaLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameCoupon>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mIndexRepository.homeCoupon(
                keyword, "0", page.toString(), limit.toString()
            )
        }
    }

    val homeCouponEnjoyLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameCoupon>>>()
    fun homeCouponEnjoy() {
        setRxLifeScope(SubscribeViewModel(homeCouponEnjoyLiveData)) {
            mIndexRepository.homeCoupon("", "1", page.toString(), "5")
        }
    }

    val homeBargainFeaturedLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBargain>>>()

    fun homeBargainFeatured() {
        setRxLifeScope(SubscribeViewModel(homeBargainFeaturedLiveData)) {
            mIndexRepository.homeBargainFeatured()
        }
    }


    val homeBargainReleaseLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBargain>>>()

    fun homeBargainRelease(keyword: String = "", sort: Int = 0, system: Int = 3) {
        setRxLifeScope(SubscribeViewModel(homeBargainReleaseLiveData)) {
            mIndexRepository.homeBargainRelease(
                keyword, sort.toString(), system.toString(), page.toString(), limit.toString()
            )
        }
    }


    val homePresentLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GamePresent>>>()
    fun homePresent(keyword: String = "", isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GamePresent>>(homePresentLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GamePresent>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mIndexRepository.homePresent(
                keyword, "0", page.toString(), limit.toString()
            )
        }
    }


    val homeServiceTodayLiveData = MutableLiveData<HttpResponseBody<ServiceTitleResponse>>()
    fun homeServiceToday() {
        setRxLifeScope(SubscribeViewModel(homeServiceTodayLiveData)) {
            mIndexRepository.homeServiceToday()
        }
    }


    val homeServiceQuickLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataM<String, MutableList<ServiceTitle>>>>()

    fun homeServiceQuick() {
        setRxLifeScope(SubscribeViewModel(homeServiceQuickLiveData)) {
            mIndexRepository.homeServiceQuick()
        }
    }

    val homeServiceHistoryLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<GameService>>>()

    fun homeServiceHistory(isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GameService>>(homeServiceHistoryLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameService>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mIndexRepository.homeServiceHistory(page.toString(), limit.toString())
        }
    }


    val homeLotteryLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<GameLottery>>>()

    fun homeLottery() {
        setRxLifeScope(SubscribeViewModel(homeLotteryLiveData)) {
            mIndexRepository.homeLottery()
        }
    }


    val homeLotteryObtainLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<LotteryObtain>>>()

    fun homeLotteryObtain(type:Int) {
        setRxLifeScope(SubscribeViewModel(homeLotteryObtainLiveData)) {
            mIndexRepository.homeLotteryObtain(type.toString())
        }
    }


    val homeLotteryRecordLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<LotteryRecord>>>()

    fun homeLotteryRecord() {
        setRxLifeScope(SubscribeViewModel(homeLotteryRecordLiveData)) {
            mIndexRepository.homeLotteryRecord(page.toString(),limit.toString())
        }
    }
}